#!/usr/bin/python3
# -*- coding: utf-8 -*-

from datetime import datetime

def size_filter(size):
    if size < 1024:
        return "%.3fb" % size
    if size < 1024 * 1024:
        return "%.3fK" % (size / 1024)
    if size < 1024 * 1024 * 1024:
        return "%.3fM" % (size / 1024 / 1024)
    if size < 1024 * 1024 * 1024 * 1024:
        return "%.3fM" % (size / 1024 / 1024 / 1024)

def range_slice(items, beg=0, end=0):
    if isinstance(items, list) or isinstance(items, str):
        return items[beg:end]
    return items


def datetime_filter(dt):
    if isinstance(dt, datetime):
        return dt.strftime("%Y-%m-%d %H:%M:%S")
    if isinstance(dt, str):
        return dt
    return datetime.now().strftime('%Y-%m-%d %H:%M:%S')


def time_filter(duration):
    duration = int(duration)
    return '00:%02d' % duration if duration < 60 else ('%02d:%02d' % (duration // 60, duration % 60) if duration < 3600 else ('%02d:%02d:%02d' % (duration // 3600, duration % 3600 // 60, duration % 3600 % 60)))

